package com.cn.wanxi.servlet;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.model.NavModel;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.service.CompanyService;
import com.cn.wanxi.service.NavService;
import com.cn.wanxi.service.NewsService;
import com.cn.wanxi.util.Person;
import javafx.application.Application;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//需要继承HttpServlet，需要引入jar包
// 所以 需要下载tomcat   启动服务的一个容器
//到底用post还是get ？区别？浏览器访问默认get方法
@WebServlet("/home")
public class HomeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


//        这里需要将数据返回给前端,前后端分离用的
//        resp.getWriter().print(JSONObject.toJSON(companyModel));
// 需要直接返回页面,页面转发--数据
//        四大作用域：page,request,session,application
//        session  会话期  启动tomcat 不能跨浏览器
//        application  应用程序   启动tomcat  可以跨浏览器
//        后台的三层:
//        CompanyService companyService = new CompanyService();
//        ServletContext application = req.getServletContext();
//        CompanyModel companyModel = new CompanyModel();
//        CompanyModel companyModel = companyService.getCompanyModel();
//        application.setAttribute("companyModel", companyModel);
//        req.setAttribute("companyModel", companyModel);
//        application. ("companyModel", companyModel);
//        key,value
//        req 请求  resp 响应
//         页面向后台发送数据 叫请求
//        后台向页面发送数据 叫响应
//        ----------------------------导航
//        navModels必须是数组！Java  数组
//        静态定义 和动态定义
//        NavService navService = new NavService();
////        NavModel[] navModels = new NavModel[6];//动态定义
//        NavModel[] navModels = navService.getNavModels();
////        NavModel[] navModels1 = {new NavModel(), new NavModel()};//静态定义
//        req.setAttribute("navModels", navModels);
        CommonServlet.getCompanyAndNavData(req);
//        ---------------------------------新闻
        NewsService newsService=new NewsService();
        NewsModel[] newsModels=newsService.getNewsModelsForHomePage();
        req.setAttribute("newsModels", newsModels);
        req.getRequestDispatcher("/jsp/home.jsp").forward(req, resp);
    }

    public static void main(String[] args) {
        Person person = new Person();
//        person
    }

}
